package com.hmall.item.web;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmall.common.config.MqConstant;
import com.hmall.common.dto.PageDTO;
import com.hmall.item.mapper.ItemMapper;
import com.hmall.item.pojo.Item;
import com.hmall.item.service.IItemService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("item")
public class ItemController {

    @Autowired
    private IItemService itemService;

    @Autowired
    RabbitTemplate rabbitTemplate;
    @Autowired
    ItemMapper itemMapper;

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping("list")
    public PageDTO<Item> list(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
        log.info("page：{},size:{}", page, size);
        Page pageMp = new Page(page, size);
        Page<Item> pageResult = itemService.page(pageMp, null);
        return new PageDTO<Item>(pageResult.getTotal(), pageResult.getRecords());
    }

    /**
     * 根据id查询，回显
     *
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Item getById(@PathVariable Long id) {
        log.info("需要查询的商品id是：{}", id);
        return itemService.findById(id);
    }

    /**
     * 修改商品状态
     *
     * @param item
     * @return
     */
    @PutMapping("/status/{id}/{status}")
    public void updateById(Item item) {
        log.info("需要修改的商品是：{}", item);
        itemService.updateById(item);
        Integer status = item.getStatus();
        if (2 == status.intValue()) {
            rabbitTemplate.convertAndSend(MqConstant.EXCHANGE_DIRECT, MqConstant.BINDINGKEY_ITEM_DELETE, item.getId().toString());
        } else if (1 == status.intValue()) {
            rabbitTemplate.convertAndSend(MqConstant.EXCHANGE_DIRECT, MqConstant.BINDINGKEY_ITEM_INSERT, item.getId().toString());
        }
    }

    /**
     * 新增商品
     *
     * @param item
     */
    @PostMapping
    public void saveItem(@RequestBody Item item) {
        // 基本数据
        item.setCreateTime(LocalDateTime.now());
        item.setUpdateTime(LocalDateTime.now());
        item.setStatus(2);
        // 新增
        itemService.save(item);
        rabbitTemplate.convertAndSend(MqConstant.EXCHANGE_DIRECT, MqConstant.BINDINGKEY_ITEM_INSERT, item.getId().toString());
    }

    /**
     * 按照id修改商品信息
     *
     * @param item
     * @return
     */
    @PutMapping
    public boolean update(@RequestBody Item item) {
        log.info("需要修改的商品是：{}", item);
        rabbitTemplate.convertAndSend(MqConstant.EXCHANGE_DIRECT, MqConstant.BINDINGKEY_ITEM_INSERT, item.getId().toString());
        return itemService.updateById(item);
    }

    /**
     * 按照id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("{id}")
    public boolean deleteById(@PathVariable Long id) {
        log.info("要删除的商品id是：{}", id);
        rabbitTemplate.convertAndSend(MqConstant.EXCHANGE_DIRECT, MqConstant.BINDINGKEY_ITEM_DELETE, id.toString());
        return itemService.removeById(id);
    }

    /**
     * 扣减库存方法
     * @param itemId
     * @param num
     */
    @PutMapping("/stock/{itemId}/{num}")
    public void updateStock(@PathVariable("itemId") Long itemId, @PathVariable("num") Integer num) {
        Item item = itemMapper.selectByIdForUpdate(itemId);
        item.setStock(item.getStock() - num);
        itemMapper.updateById(item);
    }
}
